package com.myflink.day02;

import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer011;

import java.util.Properties;

/**
 * @author Shelly An
 * @create 2020/9/16 15:04
 */
public class Source_Kafka {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        Properties prop = new Properties();
        prop.setProperty("bootstrap.servers","hadoop102:9092");
        prop.setProperty("group.id","consumer-group");
        //kafka内部的反序列化
        prop.setProperty("key.deserializer","org.apache.kafka.common.serialization.StringDeserializer");
        prop.setProperty("value.deserializer","org.apache.kafka.common.serialization.StringDeserializer");
        prop.setProperty("auto.offset.reset","latest");

        DataStreamSource<String> kafkaDS = env.addSource(
                //kafka消费者
                new FlinkKafkaConsumer011<String>(
                        //kafka topic
                        "sensor",
                        //反序列化  flink从kafka接收的字节数据的序列化，生成flink可处理的java/scala对象
                        new SimpleStringSchema(),
                        //与kafka连接的配置项
                        prop
                )
        );

        kafkaDS.print();

        env.execute();
    }
}
